<?php

namespace Crawler;

class Client {

    public function request($url, $data = array(), $method = 'get') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($method == 'post'):
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        else:
            foreach ($data as $key => $val) {
                $tmp[] = "$key=$val";
            }
            $params = join('&', $tmp);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        endif;

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch) or die(curl_error($ch));
        curl_close($ch);
        return $result;
    }

}